package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecificationVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/specificationController")
@Controller
public class SpecificationController {

    private Logger logger = LoggerFactory.getLogger(SpecificationController.class);

    @DubboReference
    private SpecificationService specificationService;

    @RequestMapping("/toShow")
    public String toShow(){
        return "/admin/specification";
    }

    @RequestMapping("/listSpecifications")
    @ResponseBody
    public Result listSpecifications(String specName){

        try {
            List<TbSpecification> list = specificationService.listSpecifications(specName);
            return Result.OK().setData(list);
        }catch (Exception e){
            logger.error("模板列表查询错误 {} ",e);
            return Result.ER();
        }
    }
    @RequestMapping("/selectSpecificationAndOptionById")
    @ResponseBody
    public Result selectSpecificationAndOptionById(Long id){
        try {
            SpecificationVo specificationVo = specificationService.selectSpecificationAndOptionById(id);
            return Result.OK().setData(specificationVo);
        }catch (Exception e){
            logger.error("模板列表回显错误 {} ",e);
            return Result.ER();
        }
    }
    @RequestMapping("/addSpecification")
    @ResponseBody
    public Result addSpecification(String specName,String optionList){
        //将字符串转换成list集合
        List<TbSpecificationOption> tbSpecificationOptions = JSONUtil.toList(optionList, TbSpecificationOption.class);
        try {
            specificationService.addSpecificationAndOption(specName,tbSpecificationOptions);
            return Result.OK();
        }catch (Exception e){
            logger.error("模板列表添加错误 {} ",e);
            return Result.ER();
        }
    }
    @RequestMapping("/updateSpecification")
    @ResponseBody
    public Result updateSpecification(String specName,String optionList,Long id) {
        //将字符串转换成list集合
        List<TbSpecificationOption> tbSpecificationOptions = JSONUtil.toList(optionList, TbSpecificationOption.class);
        try {
            specificationService.updateSpecificationAndOption(specName,tbSpecificationOptions,id);
            return Result.OK();
        }catch (Exception e){
            logger.error("模板列表添加错误 {} ",e);
            return Result.ER();
        }
    }
    @RequestMapping("/delSpecificatons")
    @ResponseBody
    public Result delSpecificatons(@RequestParam(value = "ids") List<Long> ids){
        try {
            specificationService.delSpecificatons(ids);
            return Result.OK();
        }catch (Exception e){
            logger.error("模板删除错误 {} ",e);
            return Result.ER();
        }
    }
}
